<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  <meta name="renderer" content="webkit">
  <title>登录</title>
  <link rel="stylesheet" href="/static/css/pintuer.css">
  <link rel="stylesheet" href="/static/css/admin.css">
  <script src="/static/js/jquery.js"></script>
  <script src="/static/js/pintuer.js"></script>

  <style>
    .field {
      position: relative; /* 确保父元素是相对定位的 */
    }

    .error-message {
      position: absolute;
      bottom: -20px; /* 调整显示位置 */
      left: 0;
      color: red;
    }
  </style>
</head>
<body>
<div class="bg"></div>
<div class="container">
  <div class="line bouncein">
    <div class="xs6 xm4 xs3-move xm4-move">
      <div style="height:150px;"></div>
      <div class="media media-y margin-big-bottom">
      </div>
      <form action="" id="login_form">
        <div class="panel loginbox">
          <div class="text-center margin-big padding-big-top"><h1>后台管理中心</h1></div>
          <div class="panel-body" style="padding:30px; padding-bottom:10px; padding-top:10px;">
            <div class="form-group">
              <div id="div_ukey" class="field field-icon-right">
                <input type="text" class="input input-big" name="username" id="ukey" placeholder="登录账号"
                       />
                <span class="icon icon-user margin-small"></span>
              </div>
              <span class="ukey_tips" style="color: red;"></span>
            </div>
            <div class="form-group">
              <div id="div_pwd" class="field field-icon-right">
                <input type="password" class="input input-big" name="password" id="pwd" placeholder="登录密码"
                       />
                <span class="icon icon-key margin-small"></span>
              </div>
              <span class="pwd_tips" style="color: red;"></span>
            </div>
            <div class="form-group">
              <div id="div_code" class="field">
                <input type="text" class="input input-big" name="code" id="code" placeholder="填写右侧的验证码"
                       />
                <!--<img src="RandomServlet" alt="" width="100" height="32" class="passcode"
                     style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'">-->

              </div>
              <span class="code_tips" style="color: red;"></span>
            </div>
          </div>
          <div style="padding:30px;"><input type="button" id="btn"
                                            class="button button-block bg-main text-big input-big" value="登录">
          </div>
        </div>
      </form>
    </div>
  </div>
</div>


<script>


  // 提交
  $('#btn').click(function () {
    // ajax 提交数据
    $.ajax({
      url: '/login/handler',
      type: 'POST',
      data: {
        username: $('#ukey').val().trim(),
        password: $('#pwd').val().trim(),
        code: $('#code').val().trim()
      },
      success: function (res) {
        const data = JSON.parse(res);
        console.log(data)
        if (data.code === 200) {
          // 登录成功
          window.location.href = '/login/' + data.data
        } else if (data.code === 501) {
          // 验证码错误
          console.log(data.msg)
          $('.code_tips').html('验证码错误')
          $('.ukey_tips').html('')
          $('.pwd_tips').html('')
        } else if (data.code === 502) {
          // 验证码错误
          console.log(data.msg)
          $('.ukey_tips').html('用户名错误')
          $('.pwd_tips').html('密码错误')
          $('.code_tips').html('')
        }
      },
      error: function (e) {
        console.log('有错误！！！', e)
      }

    })
  })




</script>
</body>
</html>